package com.heima.article.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.model.article.dto.ArticleLoadDto;
import com.heima.model.article.pojos.ApArticle;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ApArticleService
 *
 * @author liudo
 * @version 1.0
 * @project heima-leadnews
 * @date 2023/10/30 10:58:55
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Override
    public List<ApArticle> load(ArticleLoadDto articleLoadDto) {
        // 对于"__all __"," __dyna __"跳过频道查询 --动态SQL
        // 如果项目中使用了MyBatisPlus且Mapper中第一个参数是Page类型,就能实现分页查询
        Page<ApArticle> pageQuery = new Page<>(1,articleLoadDto.getSize(),false);   // false:不需要查总条数(性能优化)
        ApArticle apArticleQuery = new ApArticle();
        if (!StringUtils.equalsAny(articleLoadDto.getTag(),"__all__"," __dyna__")){
            apArticleQuery.setChannelId(Integer.valueOf(articleLoadDto.getTag()));
        }
        Page<ApArticle> page = baseMapper.selectPageLimit(pageQuery,apArticleQuery);
        return page.getRecords();
    }

    @Override
    public List<ApArticle> loadMore(ArticleLoadDto articleLoadDto) {
        Page<ApArticle> pageQuery = new Page<>(1,articleLoadDto.getSize(),false);
        ApArticle apArticleQuery = new ApArticle();
        if (!StringUtils.equalsAny(articleLoadDto.getTag(),"__all__"," __dyna__")){
            apArticleQuery.setChannelId(Integer.valueOf(articleLoadDto.getTag()));
        }
        apArticleQuery.setPublishTime(articleLoadDto.getMinBehotTime());
        Page<ApArticle> page = baseMapper.selectPageBeforePulishTimeLimit(pageQuery,apArticleQuery);
        return page.getRecords();
    }

    @Override
    public List<ApArticle> loadNew(ArticleLoadDto articleLoadDto) {
        Page<ApArticle> pageQuery = new Page<>(1,articleLoadDto.getSize(),false);
        ApArticle apArticleQuery = new ApArticle();
        if (!StringUtils.equalsAny(articleLoadDto.getTag(),"__all__"," __dyna__")){
            apArticleQuery.setChannelId(Integer.valueOf(articleLoadDto.getTag()));
        }
        apArticleQuery.setPublishTime(articleLoadDto.getMaxBehotTime());
        Page<ApArticle> page = baseMapper.selectPageAfterPulishTimeLimit(pageQuery,apArticleQuery);
        return page.getRecords();
    }
}
